﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using Com.BU;
using System.Media;
using System.Linq;

namespace Com
{
    /// <summary>
    /// Interaction logic for ucAirFlow.xaml
    /// </summary>
    public partial class ucAirFlow : UserControl
    {
        public ucAirFlow()
        {
            this.InitializeComponent();
            loadData();
            enableButtons();
        }

        private void loadData()
        {
            cbxDescription.Items.Clear();
            foreach (var item in LibraryManagerInCom.ExitAirList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;

        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_exiting_air ex = (tb_exiting_air)cbxDescription.SelectedItem;
            if (ex != null)
            {
                txtCoe.Text = ex.coefficient.ToString();
                txtVolume.Text = ex.room_volume.ToString();

            }
            else
            {
                txtCoe.Text = "";
                txtVolume.Text = "";
            }
        }
        private void enableButtons()
        {
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;

            btnCopy.IsEnabled = true;
            btnNew.IsEnabled = true;
            btnDelete.IsEnabled = true;

        }
        private void disableButtons()
        {
            btnSave.IsEnabled = true;
            btnCancel.IsEnabled = true;

            btnCopy.IsEnabled = false;
            btnNew.IsEnabled = false;
            btnDelete.IsEnabled = false;

        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                tb_exiting_air ex = new tb_exiting_air();
                ex.description = cbxDescription.Text;
                ex.room_volume = Convert.ToInt16(txtVolume.Text);
                ex.coefficient = Convert.ToDouble(txtCoe.Text);
                LibraryManagerInCom.addExitingAir(ex);
                loadData();
                enableButtons();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            loadData();
            enableButtons();
        }

        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxDescription.Text = "";
            txtCoe.Text = "";
            txtVolume.Text = "";
            disableButtons();

        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_exiting_air ex = LibraryManagerInCom.copyExitingAir((tb_exiting_air)cbxDescription.SelectedItem);
            cbxDescription.Text = ex.description;
            txtCoe.Text = ex.coefficient.ToString();
            txtVolume.Text = ex.room_volume.ToString();
            disableButtons();

        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question)==MessageBoxResult.OK)
            {
                try
                {
                    tb_exiting_air ex = (tb_exiting_air)cbxDescription.SelectedItem;
                    LibraryManagerInCom.delExitingAir(ex);
                    loadData();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
           
            }
            
        }

        #region Check null value
        private void CheckEmpty(object sender, TextChangedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion
    }


}